/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CL;

import EL.*;
import java.io.*;
import java.math.BigDecimal;

import java.util.*;

import javax.naming.InitialContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import slsBeans.RoomTypeBLORemote;

/**
 *
 * @author Tian
 */
public class RoomTypeCO extends HttpServlet {
    private RoomTypeBLORemote rRoomType;

    @Override
    public void init() throws ServletException
    {
        try
        {
            super.init();
            String jndiFileName = this.getServletContext().getRealPath("/") + "/WEB-INF/jndi.properties";
            Properties props = new Properties();
            props.load(new FileInputStream(jndiFileName));
            InitialContext ctx = new InitialContext(props);
            rRoomType = (RoomTypeBLORemote) ctx.lookup("RoomTypeBLO/remote");
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String action = request.getParameter("action");
        if(action.equalsIgnoreCase("formAddNew"))
        {
            response.sendRedirect("ad_RoomType_Adding.jsp");
        }
        else if(action.equalsIgnoreCase("add"))
        {
            String roomType = request.getParameter("txtRoomType");
            String description = request.getParameter("txtDescription");
            String surcharge = request.getParameter("txtSurcharge");
            RoomTypes type = new RoomTypes(roomType, description, Float.parseFloat(surcharge));
            boolean result = rRoomType.add(type);
            if(result)
            {
                response.sendRedirect("RoomTypeCO?action=getAll");
                //msg insert successfull
            }
            else
            {
                response.sendRedirect("RoomTypeCO?action=formAddNew");
                //msg insert fail
            }
        }
        else if (action.equalsIgnoreCase("getAll"))
        {
            List<RoomTypes> roomTypes = rRoomType.getAll();
            for (RoomTypes rt : roomTypes)
            {
                System.out.println(rt.getType());
            }
            request.setAttribute("roomTypes", roomTypes);
            RequestDispatcher rd = request.getRequestDispatcher("ad_RoomType_Viewing.jsp");
            rd.forward(request, response);
        }
        else if (action.equalsIgnoreCase("formEditRoomType"))
        {
            String id = request.getParameter("id");
            RoomTypes roomType = rRoomType.getByID(Integer.parseInt(id));
            request.setAttribute("roomType", roomType);
            RequestDispatcher rd = request.getRequestDispatcher("ad_RoomType_Editing.jsp");
            rd.forward(request, response);
        }
        else if(action.equalsIgnoreCase("edit"))
        {
            String id = request.getParameter("txtID");
            String type = request.getParameter("txtRoomType");
            String description = request.getParameter("txtDescription");
            String surcharge = request.getParameter("txtSurcharge");
            RoomTypes roomType = rRoomType.getByID(Integer.parseInt(id));
            if(roomType != null)
            {
                roomType.setType(type);
                roomType.setDescription(description);
                roomType.setSurcharge(Float.parseFloat(surcharge));
                boolean result = rRoomType.edit(roomType);
                if(result)
                {
                    response.sendRedirect("RoomTypeCO?action=getAll");
                }
                else
                {
                    response.sendRedirect("RoomTypeCO?action=formEditRoomType");
                }
            }
        }
        else if(action.equalsIgnoreCase("delete"))
        {
            String id = request.getParameter("id");
            boolean result = rRoomType.delete(Integer.parseInt(id));
            if(result)
            {
                response.sendRedirect("RoomTypeCO?action=getAll");
            }
            else
            {
                response.sendRedirect("RoomTypeCO?action=getAll");
            }
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
